<?php

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
require(ROOT_PATH . 'includes/lib_payment.php');
require(ROOT_PATH . 'includes/lib_order.php');

$cappay = new cappay();
$act = $_GET['act'];
if ($act == 'respond'){
	$cappay->respond();
}else{
	echo $cappay->get_code($order, $payment);
}

class cappay
{
    /**
     * 构造函数
     *
     * @access  public
     * @param
     *
     * @return void
     */
    function cappay()
    {
    }

    function __construct()
    {
        $this->cappay();
    }

    /**
     * 生成支付代码
     * @param   array   $order      订单信息
     * @param   array   $payment    支付方式信息
     */
    function get_code($order, $payment)
    {
    	$v_mid = '10514';
    	$v_rcvname = $v_mid;
    	$v_rcvaddr = '广东广州海珠区世港国际公寓11-1';
    	$v_rcvtel = '18825057067';
    	$v_rcvpost = $v_mid;
    	$v_amount = '0.01';
    	$v_ymd = date('Ymd');
    	$v_orderstatus = '1';
    	$v_ordername = $v_rcvname;
    	$v_moneytype = '0';
    	$v_url = 'http://www.drleawell.com/paytest.php?act=respond&type=1';
    	$v_producttype = urlencode("货物贸易");
    	$v_idtype = '01';
    	$v_idnumber = '445381199112230022';
    	$v_idname = urlencode('谢欣盈');
    	$v_idcountry = '156';
    	$v_idaddress = '';
    	$v_userref = '123';
        $MD5Key = 'test';     //<--支付密钥--> 注:此处密钥必须与商家后台里的密钥一致
        $v_oid = "$v_ymd-$v_mid-".date('His')."-20";
        $sourcedata = $v_moneytype.$v_ymd.$v_amount.$v_rcvname.$v_oid.$v_rcvname.$v_url;
        $v_md5info = $this->hmac_md5($MD5Key,$sourcedata);
        $def_url  = '<form method=post action="https://pay.yizhifubj.com/customer/gb/pay_bank.jsp" target="_blank">';
        $def_url .= "<input type= 'hidden' name = 'v_mid'     value= '".$v_mid."'>";     //商户编号
        $def_url .= "<input type= 'hidden' name = 'v_oid'     value= '".$v_oid."'>";         //订单编号
        $def_url .= "<input type= 'hidden' name = 'v_rcvname' value= '".$v_rcvname."'>";     //收货人姓名
        $def_url .= "<input type= 'hidden' name = 'v_rcvaddr' value= '".$v_rcvaddr."'>";     //收货人地址
        $def_url .= "<input type= 'hidden' name = 'v_rcvtel'  value= '".$v_rcvtel."'>";     //收货人电话
        $def_url .= "<input type= 'hidden' name = 'v_rcvpost'  value= '".$v_rcvpost."'>";    //收货人邮编
        $def_url .= "<input type= 'hidden' name = 'v_amount'   value= '".$v_amount."'>";     //订单总金额
        $def_url .= "<input type= 'hidden' name = 'v_ymd'      value= '".$v_ymd."'>";        //订单产生日期
        $def_url .= "<input type= 'hidden' name = 'v_orderstatus' value ='".$v_orderstatus."'>";              //配货状态
        $def_url .= "<input type= 'hidden' name = 'v_ordername'   value ='".$v_ordername."'>"; //订货人姓名
        $def_url .= "<input type= 'hidden' name = 'v_moneytype'   value ='".$v_moneytype."'>"; //币种,0为人民币,1为美元
        $def_url .= "<input type= 'hidden' name = 'v_url' value='".$v_url."'>";             //支付动作完成后返回到该url，支付结果以GET方式发送
        $def_url .= "<input type= 'hidden' name = 'v_md5info' value='".$v_md5info."'>";              //订单数字指纹
        $def_url .= "<input type= 'hidden' name = 'v_producttype'   value='".$v_producttype."'>";
        $def_url .= "<input type= 'hidden' name = 'v_idtype'   value='".$v_idtype."'>";
        $def_url .= "<input type= 'hidden' name = 'v_idnumber'   value='".$v_idnumber."'>";
        $def_url .= "<input type= 'hidden' name = 'v_idname'   value='".$v_idname."'>";
        $def_url .= "<input type= 'hidden' name = 'v_idcountry'   value='".$v_idcountry."'>";
        $def_url .= "<input type= 'hidden' name = 'v_idaddress'   value='".$v_idaddress."'>";
        $def_url .= "<input type= 'hidden' name = 'v_userref'   value='".$v_userref."'>";
        $def_url .= '<table border="1" cellpadding="4" cellspacing="1" align="center" >
      <tr bgcolor="#FFFFCC">
      <td align="center" colspan="6"><font color="red">ÇëÑ¡ÔñÒøÐÐ</font></td>
      </tr>
      <tr >
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      </tr>
      <tr bgcolor="#FFFFFF">
	  <td><input type="radio" name="v_pmode" value="3" checked="checked"/><image border=0 src="images/images/CMBC.jpg" height=20 width=100></td>
	  <td><input type="radio" name="v_pmode" value="4" /><image border=0 src="images/images/CBC.jpg" height=20 width=100></td>
      <td><input type="radio" name="v_pmode" value="9" /><image border=0 src="images/images/ICBC.jpg" height=20 width=100></td>
	  <td><input type="radio" name="v_pmode" value="14" /><image border=0 src="images/images/PAB.jpg" height=20 width=100></td>
	  <td><input type="radio" name="v_pmode" value="28"/><image border=0 src="images/images/CMSB.jpg" height=20 width=100></td>
      <td><input type="radio" name="v_pmode" value="33" /><image border=0 src="images/images/CIB.jpg" height=20 width=100></td>
      </tr>
      <tr >
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      </tr>
       <tr bgcolor="#FFFFFF">
	  <td><input type="radio" name="v_pmode" value="157" /><image border=0 src="images/images/ABC.jpg" height=20 width=100></td>
	  <td> <input type="radio" name="v_pmode" value="44" /><image border=0 src="images/images/ICGB.jpg" height=20 width=100></td>
      <td><input type="radio" name="v_pmode" value="50" /><image border=0 src="images/images/BBJ.jpg" height=20 width=100></td>
      <td><input type="radio" name="v_pmode" value="59" /><image border=0 src="images/images/PSBC.jpg" height=20 width=100></td>
      <td><input type="radio" name="v_pmode" value="60" /><image border=0 src="images/images/HXB.jpg" height=20 width=100></td>
	  <td><input type="radio" name="v_pmode" value="67" /><image border=0 src="images/images/BOC.jpg" height=20 width=100></td>
      </tr>
      <tr >
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      </tr>
       <tr bgcolor="#FFFFFF">
	  <td><input type="radio" name="v_pmode" value="69" /><image border=0 src="images/images/SPDB.jpg" height=20 width=100></td>
      <td><input type="radio" name="v_pmode" value="74" /><image border=0 src="images/images/CEB.jpg" height=20 width=100></td>
      <td><input type="radio" name="v_pmode" value="75" /><image border=0 src="images/images/BRCB.jpg" height=20 width=100></td>
      <td><input type="radio" name="v_pmode" value="83" /><image border=0 src="images/images/CBB.jpg" height=20 width=100></td>
      <td> <input type="radio" name="v_pmode" value="84" /><image border=0 src="images/images/CTTIC.jpg" height=20 width=100></td>
	  <td><input type="radio" name="v_pmode" value="85" /><image border=0 src="images/images/BC.jpg" height=20 width=100></td>
      </tr>
      <tr >
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      </tr>
       <tr bgcolor="#FFFFFF">
      <td><input type="radio" name="v_pmode" value="121" /><image border=0 src="images/images/BOS.jpg" height=20 width=100></td>
      <td><input type="radio" name="v_pmode" value="904" /><image border=0 src="images/images/UnionPay.jpg" height=20 width=100></td>
      <td></td>
      <td></td>
      <td></td>
      </tr>
      <tr >
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      </tr>
  </table>';
        $def_url .= "<input type='submit' value='提交'>";
        

        $def_url .= '</form>';


        return $def_url;
    }

    /**
     * 响应操作
     */

    function respond()
    {
    	$v_oid = $_REQUEST['v_oid'];
    	$v_pstatus = $_REQUEST['v_pstatus'];
    	$v_pstring = urldecode($_REQUEST['v_pstring']);
    	$v_pmode = urldecode($_REQUEST['v_pmode']);
    	$v_amount = $_REQUEST['v_amount'];
    	$v_moneytype = $_REQUEST['v_moneytype'];
    	$v_md5info = $_REQUEST['v_md5info'];
    	$v_md5money = $_REQUEST['v_md5money'];
    	$v_sign = $_REQUEST['v_sign'];
    	$v_count = $_REQUEST['v_count'];
    	$v_mac = $_REQUEST['v_mac'];
    	$MD5Key = 'test';
    	$log = '【'.date('Y-m-d H:i:s').'】'.json_encode($_REQUEST).PHP_EOL;
    	file_put_contents(ROOT_PATH.'log/pay_log.txt', $log,FILE_APPEND | LOCK_EX);
    	if ($_REQUEST['type'] == 1){
    		
    		$v_tempdate = explode('-', $_REQUEST['v_oid']);
    		
    		
    		//接受返回数据验证开始
    		//v_md5info验证
    		$md5info_paramet = $v_oid.$v_pstatus.$v_pstring.$v_pmode;
    		$md5info_tem     = $this->hmac_md5($MD5Key,$md5info_paramet);
    		
    		//v_md5money验证
    		$md5money_paramet = $v_amount.$v_moneytype;
    		$md5money_tem     = $this->hmac_md5($MD5Key,$md5money_paramet);
    		if ($md5info_tem == $v_md5info && $md5money_tem == $v_md5money)
    		{
    			if($v_pstatus=='20')
    			{
    				echo 'success';
    			}
    			else if($v_pstatus=='30')
    			{
    				echo 'fail';
    			}
    			else
    			{
    				echo 'waiting';
    			}
    		}
    		else
    		{
    			echo 'fail';
    		}
    	}else {
    		
    		
    		$sp = '|_|';
    		$a_oid = explode($sp, $v_oid);
    		$a_pmode = explode($sp, $v_pmode);
    		$a_pstatus = explode($sp, $v_pstatus);
    		$a_pstring = explode($sp, $v_pstring);
    		$a_amount = explode($sp, $v_amount);
    		$a_moneytype = explode($sp, $v_moneytype);
    		
    		$data1 = $v_oid.$v_pmode.$v_pstatus.$v_pstring.$v_count;
    		$mac = $this->hmac_md5($MD5Key, $data1);
    		
    		$data2 = $v_amount.$v_moneytype;
    		$md5money = $this->hmac_md5($MD5Key, $data2);
    		
    		if($mac == $v_mac or $md5money == $v_md5money)
    		{
    			echo("sent");
    			for($i=0;$i<$v_count;$i++)
    			{
    				if($a_pstatus[$i]=='1')
    				{
    					$log = '【'.date('Y-m-d H:i:s').'】['.$a_oid[$i].']支付完成'.PHP_EOL;
    					file_put_contents(ROOT_PATH.'log/pay_log.txt', $log,FILE_APPEND | LOCK_EX);
    					
    					$filename = ROOT_PATH.'images/qrcode/'.$a_oid[$i].'.jpg';
    					if (file_exists($filename)){
    						@unlink($filename);
    					}
    				}
    				else if($a_pstatus[$i]=='3')
    				{
    					$log = '【'.date('Y-m-d H:i:s').'】['.$a_oid[$i].']支付被拒绝'.PHP_EOL;
    					file_put_contents(ROOT_PATH.'log/pay_log.txt', $log,FILE_APPEND | LOCK_EX);
    				}
    				else
    				{
    					$log = '【'.date('Y-m-d H:i:s').'】['.$a_oid[$i].']'.$a_pstatus[$i].PHP_EOL;
    					file_put_contents(ROOT_PATH.'log/pay_log.txt', $log,FILE_APPEND | LOCK_EX);
    				}
    		
    			}
    		}
    		else
    		{
    			echo("error");
    			$log = '【'.date('Y-m-d H:i:s').'】['.$a_oid[$i].']验证错误'.PHP_EOL;
    			file_put_contents(ROOT_PATH.'log/pay_log.txt', $log,FILE_APPEND | LOCK_EX);
    		}
    	}
    	

    }
    function hmac_md5($key, $data)
    {
        if (extension_loaded('mhash'))
        {
            return bin2hex(mhash(MHASH_MD5, $data, $key));
        }

        // RFC 2104 HMAC implementation for php. Hacked by Lance Rushing
        $b = 64;
        if (strlen($key) > $b)
        {
            $key = pack('H*', md5($key));
        }
        $key  = str_pad($key, $b, chr(0x00));
        $ipad = str_pad('', $b, chr(0x36));
        $opad = str_pad('', $b, chr(0x5c));

        $k_ipad = $key ^ $ipad;
        $k_opad = $key ^ $opad;

        return md5($k_opad . pack('H*', md5($k_ipad . $data)));
    }

}

?>